var g_context;
var stage;

var DIRECTION = {
	TOP: 'TOP',
	RIGHT: 'RIGHT',
	BOTTOM: 'BOTTOM',
	LEFT: 'LEFT'
};

var GAME_STATE = {
	PLAYING: 'Playing',
	GAME_OVER: 'GameOver',
	PAUSED: 'Paused',
	RESTART: 'Restart'
};

var Main = Base.extend({
	
	currentScore: 0,
	scoresContainer: null,
	controlButton: null,
	gameField: null,
	userInterface: null,
	
	constructor: function()
	{
		this.initGlobals();
		this.scoresContainer = $('#score');
		this.userInterface = new UserInterface();
		this.userInterface.addListener('gameStateChange', this, this.onGameStateChange);
		
		this.gameField = new GameField(this.userInterface);
		this.gameField.addListener('score', this, this.onScore);
	},

	initGlobals: function()
	{
		var snakeCanvas = document.getElementById('snakeCanvas');
		g_context = snakeCanvas.getContext('2d');
		stage = new Stage(snakeCanvas);
	},
		
	onGameStateChange: function(gameState)
	{
		if (gameState == GAME_STATE.RESTART)
		{
			this.currentScore = 0;
			this.scoresContainer.text(0);
		}
	},
			
	onScore: function(evt)
	{
		this.currentScore += 5;
		this.scoresContainer.text(this.currentScore);
	}
	
});

window.onload = function() {
	var main = new Main();
};